﻿using System;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
using System.Windows.Media;
using CI.Models.Data;
using CI.Models.Production;
using CI.Models.Production.Interfaces;
using Microsoft.Practices.ServiceLocation;

namespace CI.Utility
{
    public class CCPResourceConverter : IValueConverter
    {
        private static readonly IStaticDataRepository<Item> _itemRepo =
            ServiceLocator.Current.GetInstance<IStaticDataRepository<Item>>();


        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // value should be an Item

            var item = value as Item;

            if (item == null) throw new ArgumentException("value must be of type Item");

            // has it got a blueprint?
            if (item.Blueprint != null)
            {
                var blueprintName = item.TypeName + " Blueprint";
                var blueprint = _itemRepo.All.Where(i => i.TypeName == blueprintName).FirstOrDefault();

                if (blueprint == null) return null;

                return ResourceHelper.LoadResource<ImageSource>("Resources/CCP/blueprints/" + blueprint.TypeID + ".png");
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}